package org.aeon.client.view;

import lombok.extern.slf4j.Slf4j;
import org.aeon.client.context.GlobalContext;
import org.aeon.client.view.components.ApplicationLayeredPanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @author: MissingAeon
 * @date: 2024/2/20 8:43
 * @description:
 */
@Slf4j
public class ApplicationUI extends JFrame {

    private ApplicationLayeredPanel layeredPanel;

    public ApplicationUI() {
        GlobalContext.getContext().setMainUI(this);
        init();
    }

    private void init() {
        this.setTitle("畅聊");
        this.setSize(1080, 730);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.setResizable(true);
        // 居中
        this.setLocationRelativeTo(null);
        // 添加主界面
        layeredPanel = new ApplicationLayeredPanel();
        this.getContentPane().add(layeredPanel);
        // 请求焦点
        this.requestFocusInWindow();
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                GlobalContext.getContext().shutdownGracefully();
            }
        });
    }

    /**
     * 将组件加入窗口的弹窗图层
     *
     * @param component
     */
    public void addPopUpLayer(Component component) {
        layeredPanel.addPopUpLayer(component);
    }

    /**
     * 将组件加入窗口的弹窗图层
     *
     * @param component
     * @param position
     */
    public void addPopUpLayer(Component component, int position) {
        layeredPanel.addPopUpLayer(component, position);
    }

    /**
     * 将组件从弹窗图层删除
     *
     * @param component
     */
    public void removePopUpLayer(Component component) {
        layeredPanel.removePopUpLayer(component);
    }
}
